package eu.smoothit.sis.admin.old;

import java.util.ArrayList;
import java.util.List;

import eu.smoothit.sis.admin.backendBean.superclass.DataTableAbstract;
import eu.smoothit.sis.db.SisDAOFactory;
import eu.smoothit.sis.db.api.daos.IComponentConfigDAO;
import eu.smoothit.sis.db.impl.entities.ComponentConfigEntry;

public class ComponentConfigBean extends
		DataTableAbstract<ComponentConfigEntry> {
	// Constants
	// ----------------------------------------------------------------------------------
	public static final String COMPONENT_NAME_IOP = eu.smoothit.sis.init.web.SisWebInitializer.COMPONENT_NAME_CONTR_IOP;// "Controler-IoP-Config"
	public static final String COMPONENT_NAME_GENRIC = "PARAM_GENERIC_PEER_RATING";

	// define DAO
	private IComponentConfigDAO dataDAO = (IComponentConfigDAO) SisDAOFactory
			.getFactory().createComponentConfigDAO();
	// instance of ComponentConfigEntry, stand for query entry
	private ComponentConfigEntry searchExample;
	private List<ComponentConfigEntry> apiQuestions;

	// initialization
	public ComponentConfigBean() {
		searchExample = new ComponentConfigEntry();
		actionLoad();
	}

	// setter and getter
	// ----------------------------------------------------------------------------------
	public ComponentConfigEntry getSearchExample() {
		return searchExample;
	}

	public void setSearchExample(ComponentConfigEntry searchExample) {
		this.searchExample = searchExample;
	}

	// form actions
	// ----------------------------------------------------------------------------------
	/*
	 * save new added entries or modified exiting entry
	 */
	@Override
	public void actionSave() {
		List<ComponentConfigEntry> editItems = new ArrayList<ComponentConfigEntry>();
		for (int index = 0; index < dataList.size(); index++) {
			if (isEditModeRow(index)) {
				editItems.add(dataList.get(index));
			}
		}
		try {
			for (ComponentConfigEntry editItem : editItems) {
				if (editItem.getId() == 0)
					// id=0 stand for new added entry
					dataDAO.persist(editItem);
				else
					// otherwise, it is existing entry, update it.
					dataDAO.update(editItem);
			}
		} catch (Exception e) {
			setErrorMessage(e.getMessage() + " Cause: " + e.getCause());
			e.printStackTrace();
			return;
		}
		// Toggle edit mode
		editMode = false;
		// reload data.
		actionLoad();
	}

	@Override
	public void actionDelete() {
		List<ComponentConfigEntry> deleteItems = new ArrayList<ComponentConfigEntry>();
		for (int index = 0; index < dataList.size(); index++) {
			// Get selected items.
			if (isSelectedRow(index)) {
				deleteItems.add(dataList.get(index));
			}
		}
		if (deleteItems.isEmpty()) {
			setErrorMessage("Select at least one item to delete.");
			return;
		}
		try {
			for (ComponentConfigEntry deleteItem : deleteItems)
				dataDAO.remove(deleteItem);
		} catch (Exception e) {
			setErrorMessage(e.getMessage() + " Cause: " + e.getCause());
			e.printStackTrace();
			return;
		}
		// Reload data and go to first page.
		actionLoad();
		pageFirst();
	}

	@Override
	public void actionLoad() {
		ComponentConfigEntry question = new ComponentConfigEntry();
		if (isSearchMode()) {
			guiSearch(question);
			// setSearchMode(false);
		} else if (isMultiSearchMode()) {
			multiSearch();
			// setMultiSearchMode(false);
		} else {
			try {
				dataList = dataDAO.getAll();
			} catch (Exception e) {
				setErrorMessage(e.getMessage() + " Cause: " + e.getCause());
				e.printStackTrace();
				return;
			}
		}
		actionSort(null);
	}

	public void guiSearch(ComponentConfigEntry question) {
		boolean isQuestion = false;
		// question = new ComponentConfigEntry();
		// IComponentConfigDAO API support query of component and/or
		// property name but not property value only.
		String component = searchExample.getComponent();
		String propName = searchExample.getPropName();
		if (component.trim().length() != 0) {
			question.setComponent(component);
			isQuestion = true;
		}
		if (propName.trim().length() != 0) {
			question.setPropName(propName);
			isQuestion = true;
		}
		if (isQuestion) {
			try {
				dataList = dataDAO.get(question);
			} catch (Exception e) {
				setErrorMessage(e.getMessage() + " Cause: " + e.getCause());
				e.printStackTrace();
				// return false;
			}
		} else
			setErrorMessage("Please enter at least one keyword for searching");
	}

	public void multiSearch() {
		List<ComponentConfigEntry> tempList = new ArrayList<ComponentConfigEntry>();
		dataList.clear();
		for (ComponentConfigEntry question : apiQuestions) {
			try {
				tempList = dataDAO.get(question);
				if (tempList.size() > 1) {
					for(ComponentConfigEntry cc:tempList){
						dataList.add(cc);
					}
					return;
				} else if (tempList.size() == 1) {
					dataList.add(tempList.get(0));
				} else {
					setErrorMessage("In order to show generic peer rating records, insert record with Component name='PARAM_GENERIC_PEER_RATING'");
				}
				
			} catch (Exception e) {
				setErrorMessage(e.getMessage() + " Cause: " + e.getCause());
				e.printStackTrace();
				return;
			}
			tempList.clear();
		}
	}

	public void configIOP() {
		//TODO validate value is Integer, 
		// setSearchMode(true);
		setMultiSearchMode(true);
		apiQuestions = new ArrayList<ComponentConfigEntry>();
		ComponentConfigEntry question = new ComponentConfigEntry();
		// web : searchExample.setPropName="timespan"/"topTorrents"
		String component = COMPONENT_NAME_IOP;
//		String propName = IOP_Nr_TOP_TORRENTS;
		// TODO add via map type
		question.setComponent(component);
		apiQuestions.add(question);
		//TODO replace with a loop
		apiQuestions.add(question);
		actionLoad();
		pageFirst();

	}
	
	public void configGenericPeerRatingParameters() {
		setMultiSearchMode(true);
		apiQuestions = new ArrayList<ComponentConfigEntry>();
		ComponentConfigEntry question = new ComponentConfigEntry();
		String component = COMPONENT_NAME_GENRIC;
		question.setComponent(component);
		apiQuestions.add(question);
		apiQuestions.add(question);
		actionLoad();
		pageFirst();

	}

	/**
	 * overload actionAdd(E) as JSF web page allows methods without parameters
	 * only.
	 */
	public void actionAdd() {
		actionAdd(new ComponentConfigEntry());
	}

	@Override
	public void actionRefresh() {
		searchExample = new ComponentConfigEntry();
		super.actionRefresh();
	}



}
